﻿using System;
using System.Collections.Generic;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Umi.Crypto.Abstraction.HashAlgo;
using Umi.Web.Metadatas.Attributes;

namespace Umi.Web.Crypto.Hash
{
    [Service("hashFactory", Scoped = ServiceScoped.Single)]
    public class HashFactory : IHashFactory
    {
        public IHashService GetHash(string hashType)
        {
            return new HashService(hashType);
        }
    }

    /// <summary>
    /// 通用Hash
    /// </summary>
    public class HashService : IHashService
    {
        private readonly IDigest _digest;

        public HashService(string hashType)
        {
            _digest = DigestUtilities.GetDigest(hashType);
        }

        public string Name => _digest.AlgorithmName;

        public byte[] ComputeHash(byte[] data)
        {
            byte[] rtn = new byte[_digest.GetDigestSize()];
            _digest.BlockUpdate(data, 0, data.Length);
            _digest.DoFinal(rtn, 0);
            return rtn;
        }
    }
}
